package com.zzty.zzlp.controller;

import com.zzty.zzlp.dao.ClazzMapper;
import com.zzty.zzlp.dao.UserDao;
import com.zzty.zzlp.entity.Clazz;
import com.zzty.zzlp.entity.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/Placement")
public class PlacementController {

    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    UserDao userDao;


    @RequestMapping(value = "getall", method = RequestMethod.GET)
    @ResponseBody
    public String getAllStudent(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        List<User> users = userDao.searchAllStudent();
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getDisname() != null) {
                System.out.println(users.get(i).getDisname());
                jsonObject.put("id", users.get(i).getId());
                jsonObject.put("name", users.get(i).getDisname());
                jsonArray.add(jsonObject);
            }
        }
        return jsonArray.toString();
    }

    @RequestMapping(value = "getclass", method = RequestMethod.GET)
    @ResponseBody
    public String getAllClass(HttpServletRequest request) throws Exception {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        List<Clazz> clazzs = clazzMapper.getAllClazz();

        for (int i = 0; i < clazzs.size(); i++) {
            System.out.println(clazzs.get(i).getName());
            jsonObject.put("id", clazzs.get(i).getId());
            jsonObject.put("name", clazzs.get(i).getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray.toString();
    }

    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    public void confirmPlacement(@RequestParam(value = "id") int id,
                                 @RequestParam(value = "clazz") int clazzId,
                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println(id + "==" + clazzId);
        User user = new User();
        user.setId(id);
        user.setClazzId(clazzId);
        userDao.changeClazzById(user);
    }

}
